package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Category;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.Feed;
import hu.rsc.feeds.client.data.UserInfo;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class FeedForm extends Composite {
    private FeedsPanel parent;

    private FlexTable form = new FlexTable();

    private TextBox titleTb;
    private TextBox urlTb;
    private ListBox langList = new ListBox();
    private ListBox categoryList = new ListBox();
    private TextArea newFeedDesc = new TextArea();

    private Feed feed = new Feed();

    public FeedForm(FeedsPanel parent) {
	this.parent = parent;
	buildContent();
	initWidget(this.form);
    }

    public Feed getFeed() {
	return this.feed;
    }

    public void refresh(Feed feed) {
	this.feed = feed;
	buildContent();
    }

    private void buildContent() {
	this.form.clear();
	this.form.addStyleName("form");
	this.form.addStyleName("border");
	int row = 0;

	Label lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_CHANEL_name, lb));
	this.form.setWidget(row, 0, lb);
	this.titleTb = new TextBox();
	this.titleTb.addStyleName("forminput");
	this.form.setWidget(row, 1, this.titleTb);
	this.titleTb.setValue(this.feed.getLabel());
	row++;

	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_CHANEL_URL, lb));
	this.form.setWidget(row, 0, lb);
	this.urlTb = new TextBox();
	this.urlTb.setReadOnly(this.feed.getId() != null && this.feed.getId() > 0l);
	this.urlTb.addStyleName("forminput");
	this.form.setWidget(row, 1, this.urlTb);
	this.urlTb.setValue(this.feed.getUrl());
	row++;

	this.langList = new ListBox();
	this.langList.addItem(UserInfo.LANG_EN);
	this.langList.addItem(UserInfo.LANG_HU);
	if (this.feed.getLanguage() == null || this.feed.getLanguage().equals(UserInfo.LANG_EN)) {
	    this.langList.setSelectedIndex(0);
	} else {
	    this.langList.setSelectedIndex(1);
	}
	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_LANG, lb));
	this.form.setWidget(row, 0, lb);
	this.form.setWidget(row, 1, this.langList);
	this.langList.addChangeHandler(new ChangeHandler() {

	    @Override
	    public void onChange(ChangeEvent event) {
		FeedForm.this.categoryList.clear();
		String lang = FeedForm.this.langList.getItemText(FeedForm.this.langList.getSelectedIndex());
		for (Category c : Feeds.getInitData().getCategories()) {
		    if (c.getLanguage().equals(lang)) {
			FeedForm.this.categoryList.addItem(c.getLabel(), c.getId().toString());
		    }
		}
	    }
	});
	row++;

	String lang = this.langList.getItemText(this.langList.getSelectedIndex());

	this.categoryList = new ListBox();
	int cindex = 0;
	for (Category c : Feeds.getInitData().getCategories()) {
	    if (c.getLanguage().equals(lang)) {
		this.categoryList.addItem(c.getLabel(), c.getId().toString());
	    }
	    if (this.feed.getCategory() != null && this.feed.getCategory().equals(c.getId())) {
		this.categoryList.setSelectedIndex(cindex);
	    }
	    cindex++;
	}

	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_CATEGORY, lb));
	this.form.setWidget(row, 1, this.categoryList);
	this.form.setWidget(row, 0, lb);
	row++;

	this.newFeedDesc = new TextArea();
	this.newFeedDesc.addStyleName("forminput");
	this.newFeedDesc.addStyleName("textarea");
	this.newFeedDesc.setValue(this.feed.getDescription());
	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_DESCRIPTION, lb));
	this.form.setWidget(row, 0, lb);
	row++;
	this.form.setWidget(row, 0, this.newFeedDesc);
	this.form.getFlexCellFormatter().setColSpan(row, 0, 2);
	row++;

	HorizontalPanel buttonPanel = new HorizontalPanel();
	buttonPanel.addStyleName("buttonpanel");
	this.form.setWidget(row, 0, buttonPanel);
	this.form.getFlexCellFormatter().setColSpan(row, 0, 2);

	Button save = new Button();
	save.setText(Dictionary.getEntry(Dictionary.BUTTON_EDIT_PROFILE, save));
	buttonPanel.add(save);

	save.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		if (validate()) {
		    FeedForm.this.parent.save(FeedForm.this.feed);
		}
	    }
	});

	Button newBt = new Button();
	newBt.setText(Dictionary.getEntry(Dictionary.BUTTON_NEW_CHANNEL, save));
	buttonPanel.add(newBt);
	newBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		refresh(new Feed());
	    }
	});

    }

    private boolean validate() {
	this.feed.setLabel(this.titleTb.getValue());
	this.feed.setDescription(this.newFeedDesc.getValue());
	this.feed.setUrl(this.urlTb.getValue());
	this.feed.setLanguage(this.langList.getItemText(this.langList.getSelectedIndex()));
	try {
	    this.feed.setCategory(Long.parseLong(this.categoryList.getValue(this.categoryList.getSelectedIndex())));
	} catch (Exception e) {
	    this.feed.setCategory(null);
	}

	StringBuilder error = new StringBuilder();
	if (this.feed.getLabel() == null || this.feed.getLabel().length() == 0) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_TILE_REQUIRED, null));
	}
	if (this.feed.getUrl() == null || this.feed.getUrl().length() == 0) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_INVALID_DATAS, null));
	}

	Category category = Feeds.getCategoryById(this.feed.getCategory());

	if (category != null && !category.getLanguage().equals(this.feed.getLanguage())) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_DIFFERENT_LANGUAGE, null));
	}

	if (error.length() > 0) {
	    Window.alert(error.toString());
	    return false;
	}
	return true;
    }
}
